package practice1_100;

import java.util.*;

public class Practice1337 {
    static char a;
    public int[] kWeakestRows(int[][] mat, int k) {
        Integer res[][] =new Integer[mat.length][2];
        int[] result=new int[k];
        for (int i = 0; i < mat.length ; i++) {
            int count=0;
            for (int j = 0; j <mat[0].length ; j++) {
                if(mat[i][j]==0)break;
                count+=1;
            }
            res[i][0]=i;
            res[i][1]=count;
        }
        Arrays.sort(res, new Comparator<Integer[]>() {
            @Override
            public int compare(Integer[] o1, Integer[] o2) {
                if(o1[1]==o2[1]) return o1[0]-o2[0];
                return o1[1]-o2[1];
            }
        });

        for (int i = 0; i < result.length; i++) {
            result[i]=res[i][0];
        }
        return result;
    }

    public static int[] reversePrint(ListNode head) {
        int num=0;
        if(head==null) return new int[0];
        ListNode tempHead=new ListNode(0);
        ListNode temp=head;
        while(head!=null){
            num++;
            head=head.next;
            temp.next= tempHead.next;
            tempHead.next=temp;
        }
        head=tempHead.next;
        int[] res=new int[num];
        int a=0;
        while(head!=null){
            res[a]=head.val;
            a++;
            head=head.next;
        }
        return res;
    }

    public static ListNode reverseList(ListNode head) {
        ListNode temp =head,tempHead=new ListNode(0);
        while(temp!=null){
            temp.next=tempHead.next;
            tempHead.next=temp;
            head=head.next;
            temp=head;
        }
        return tempHead.next;
    }

    public static void main(String[] args) {
        //[1,3,2]
        Practice1337.reverseList(new ListNode(1,new ListNode(3 ,new ListNode(2))));

    }
}
